package com.example.iotmanagerment26;

import com.example.iotmanagerment26.domain.Data;
import com.example.iotmanagerment26.domain.JsonData;
import com.example.iotmanagerment26.service.DataService;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@Component
public class OkHttpExample1 {

    // only one client, singleton, better puts it in a factory,
    // multiple instances will create more memory.
    public final OkHttpClient httpClient = new OkHttpClient();
    ArrayList<String> list_str = new ArrayList<String>(); //String的Lis
    String sss;

    @Resource
    Gson gson;
    @Resource
    Data data;

    @Resource
    DataService dataService;

    public JsonData sendPOSTSync(String posturl, String reqJsonStr)  {
        Request request = new Request.Builder()
                .url(posturl)
                .post(RequestBody.create(MediaType.parse("application/json"), reqJsonStr))
                .build();
        try (Response response = httpClient.newCall(request).execute()) {
            String sss=response.body().string();
            System.out.println(sss);
            return new JsonData(1,sss,"请求成功");
        }
        catch (IOException e){
            return new JsonData(0,e.toString(),"请求失败");
        }
    }

    public void sendGETSync() throws IOException {
        Request request = new Request.Builder()
                .url("http://129.204.4.65:30005/get_teststr")
                .build();

        try (Response response = httpClient.newCall(request).execute()) {

            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);



            // Get response body
            sss=response.body().string();
            System.out.println(sss);
            int len1=0;
            if (sss.contains("\n")) {
                list_str=new ArrayList<String>(Arrays.asList(sss.split("\n")));
                len1=list_str.size();
            }
            System.out.println(len1);
            if(len1>=1) {
                for(int k=0;k<len1;k++){
                    System.out.println(list_str.get(k).trim());
                    if(list_str.get(k).trim().contains("{")){
                        String json = list_str.get(k).trim().replace("deviceid","devid");
                        System.out.println("json:   "+json);
                        data=gson.fromJson(json,Data.class);
                        System.out.println(data);
                        data.setTime(new Date());
                        int result=dataService.insertData(data);
                        if(result>=1){
                            System.out.println("第"+(k+1)+"条记录写入成功");
                        }
                    }else{
                        System.out.println("逗号:   "+list_str.get(k).trim());
                        String []ds=list_str.get(k).trim().split(",");
                        data.setName(ds[1]);
                        double t=Double.parseDouble(ds[3]);
                        data.setTemperature(t);
                        double h=Double.parseDouble(ds[5]);
                        data.setHumidity(h);
                        double l=Double.parseDouble(ds[4]);
                        data.setLight(l);
                        data.setTime(new Date());
                        int result=dataService.insertData(data);
                        if(result>=1){
                            System.out.println("第"+(k+1)+"条记录写入成功");
                        }
                    }
                }

            }
            //
            /*
            补充写数据库


            */
        }

    }

}
